/**
  ******************************************************************************
  * @file    dac.c
  * @author  ashuai0110
  * @version V1.0
  * @date    2024-01-12
  * @brief   dac驱动,适用于PIC16(L)F1788_9
  *
  ******************************************************************************
  * @attention
  *
  * Change Logs:
  * Date          Author        Notes
  * 2024-01-12    ashuai0110    完成基本内容
  *
  ******************************************************************************
  */

/* 包含头文件-----------------------------------------------------------------*/
#include "dac.h"

/* 私有宏定义-----------------------------------------------------------------*/

/* 私有类型定义---------------------------------------------------------------*/

/* 私有变量-------------------------------------------------------------------*/

/* 全局变量-------------------------------------------------------------------*/

/* 私有函数原型---------------------------------------------------------------*/

/* DAC1OUT1输出举例
    dac_config_t dacConfig;
    dacConfig.outCh = DAC_OUT_1_ON_2_OFF;
    dacConfig.pss = DAC_PSS_VDD;
    dacConfig.nss = DAC_NSS_VSS;
    dac_init(DAC1, dacConfig);
    dac_set_value(DAC1, 0x7F);
    dac_cmd(DAC1, 1);
 */

/**
  * @brief  dac初始化
  * @param  dacx   : dac通道 @ref dacx
  * @param  config : dac配置参数
  * @retval None
  */
void dac_init(uint8_t dacx, dac_config_t config)
{
    uint8_t tmp;
    
    if(DAC1 != dacx) { config.nss = DAC_NSS_VSS; }
    tmp = (config.outCh << 4) | (config.pss << 2) | (config.nss);
    switch(dacx)
    {
        case DAC1: DAC1CON0 = tmp; break;
        case DAC2: DAC2CON0 = tmp; break;
        case DAC3: DAC3CON0 = tmp; break;
        case DAC4: DAC4CON0 = tmp; break;
    }
}

/**
  * @brief  dac取消初始化(恢复缺省)
  * @param  dacx : dac通道 @ref dacx
  * @retval None
  */
void dac_de_init(uint8_t dacx)
{
    switch(dacx)
    {
        case DAC1: DAC1CON0 = 0; break;
        case DAC2: DAC2CON0 = 0; break;
        case DAC3: DAC3CON0 = 0; break;
        case DAC4: DAC4CON0 = 0; break;
    }
}

/**
  * @brief  dac控制
  * @param  dacx  : dac通道 @ref dacx
  * @param  state : 0关闭 1开启
  * @retval None
  */
void dac_cmd(uint8_t dacx, uint8_t state)
{
    switch(dacx)
    {
        case DAC1: DAC1EN = state; break;
        case DAC2: DAC2EN = state; break;
        case DAC3: DAC3EN = state; break;
        case DAC4: DAC4EN = state; break;
    }
}

/**
  * @brief  dac设置值
  * @param  dacx  : dac通道 @ref dacx
  * @param  value : 0~255
  * @retval None
  */
void dac_set_value(uint8_t dacx, uint8_t value)
{
    switch(dacx)
    {
        case DAC1: DAC1CON1 = value; break;
        case DAC2: DAC2CON1 = value & 0x1F; break;
        case DAC3: DAC3CON1 = value & 0x1F; break;
        case DAC4: DAC4CON1 = value & 0x1F; break;
    }
}



